﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchDirect : UserControl, InterfaceSearchControl
    {
        private bool _isNotFill = true;
        private bool _openOnDblClick = false;
        //private int _pid = -1;

        public UserControlSearchDirect()
        {
            InitializeComponent();
        }

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataSetFSO.VIEW_SEARCH_DIRECTRow dr;
                DataRow returnRow;
                if (radGridViewList.SelectedRows.Count > 0)
                {
                    dr = (DataSetFSO.VIEW_SEARCH_DIRECTRow)(((DataRowView)(radGridViewList.SelectedRows.First()).DataBoundItem)).Row;
                    returnRow = directTableAdapter1.GetDataByID(dr.ID).FindByID(dr.ID);
                }
                else
                {
                    returnRow = null;
                }
                return returnRow;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { _isNotFill = false; dataSetFSO1 = (DataSetFSO)value; InitializeGridView(); }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewList.SelectedRows;
            }
        }

        public void RefreshList()
        {
            vieW_SEARCH_DIRECTTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_DIRECT);
        }

        private void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn("NUMBER", dataSetFSO1.VIEW_SEARCH_DIRECT.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewDateTimeColumn colDate = new GridViewDateTimeColumn("DATE", dataSetFSO1.VIEW_SEARCH_DIRECT.DATEColumn.ColumnName);
            colDate.HeaderText = "Дата";
            colDate.FormatString = "{0:d}";

            GridViewTextBoxColumn colTypeOfAct = new GridViewTextBoxColumn("TYPE_OF_ACTIVITY", dataSetFSO1.VIEW_SEARCH_DIRECT.NSI_TYPE_OF_ACTIVITY_NAMEColumn.ColumnName);
            colTypeOfAct.HeaderText = "Направление";

            GridViewTextBoxColumn colOrg = new GridViewTextBoxColumn("ORGANIZATION", dataSetFSO1.VIEW_SEARCH_DIRECT.NSI_ORGANIZATION_SHORT_NAMEColumn.ColumnName);
            colOrg.HeaderText = "Организация";

            GridViewTextBoxColumn colContract = new GridViewTextBoxColumn("CONTRACT", dataSetFSO1.VIEW_SEARCH_DIRECT.CONTRACT_NUMColumn.ColumnName);
            colContract.HeaderText = "Договор";

            GridViewTextBoxColumn colGroups = new GridViewTextBoxColumn("PROD_GROUPS", dataSetFSO1.VIEW_SEARCH_DIRECT.PROD_GROUPSColumn.ColumnName);
            colGroups.HeaderText = "Группы товаров";

            radGridViewList.MasterTemplate.Columns.Clear();
            radGridViewList.MasterTemplate.Columns.Add(colNumber);
            radGridViewList.MasterTemplate.Columns.Add(colDate);        
            radGridViewList.MasterTemplate.Columns.Add(colOrg);
            radGridViewList.MasterTemplate.Columns.Add(colContract);
            radGridViewList.MasterTemplate.Columns.Add(colTypeOfAct);
            radGridViewList.MasterTemplate.Columns.Add(colGroups);

            radGridViewList.DataSource = dataSetFSO1.VIEW_SEARCH_DIRECT;
        }

        private void UserControlSearchDirect_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                RefreshList();            
            }
            InitializeGridView();
        }

        private void radGridViewList_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            DataSetFSO.DIRECTRow selectedRow = (DataSetFSO.DIRECTRow)this.SelectedRow;
            if (selectedRow != null && _openOnDblClick)
            {
                FormDirect formDirect = new FormDirect(selectedRow);
                if (!FormHelper.HasOpenedForm(formDirect.FormName))
                {
                    formDirect.MdiParent = FormHelper.MainForm;
                    formDirect.Show();
                    formDirect.Select();
                }
            }
        }
    }
}
